using System.Web.Mvc;
using IGMVC.Web.Models;
using Infragistics.UltraChart.Resources.Appearance;

namespace IGMVC.Web.Controllers
{
    public class IssuesController : Controller
    {
        private IssueRepository _issues = null;
        private IssueTypeRepository _issueTypes = null;

        public IssuesController()
        {
            this._issues = new IssueRepository();
            this._issueTypes = new IssueTypeRepository();
        }

        public ActionResult Index()
        {
            var vm = new IssueEntryForm();
            vm.IssueTypes = this._issueTypes.GetAll();

            vm.SeverityMin = this._issues.GetMinimumSeverityValue();
            vm.SeverityMax = this._issues.GetMaximumSeverityValue();

            return View(vm);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Persist([IssueRichBinder] Issue issue)
        {
            this._issues.Persist(issue);
            var result = this.RedirectToAction("List");
            return result;
        }

        public ActionResult List(int? pg)
        {
            IssueListView vm = new IssueListView();

            vm.IssueSeverity = new NumericSeries() { Label = "Issue Serverity" };
            vm.IssueSeverity.Points.Add(new NumericDataPoint(this._issues.GetExtremelySevereIssueCount(), "Extremely Severe", false));
            vm.IssueSeverity.Points.Add(new NumericDataPoint(this._issues.GetSomewhatSevereIssueCount(), "Somewhat Severe", false));
            vm.IssueSeverity.Points.Add(new NumericDataPoint(this._issues.GetModeratelySevereIssueCount(), "Moderately Severe", false));
            vm.IssueSeverity.Points.Add(new NumericDataPoint(this._issues.GetLowSevereIssueCount(), "Low Severity", false));
            vm.IssueSeverity.Points.Add(new NumericDataPoint(this._issues.GetNotSevereIssueCount(), "Not Severe", false));

            vm.Issues = this._issues.GetAll();
            vm.PageIndex = (pg.HasValue) ? pg.Value-1 : 0;
            vm.PageSize = 5;
            vm.NumberOfPages = MathHelper.RoundUpToWholeNumber(vm.Issues.Count / vm.PageSize);
            vm.ActionName = "List";
            vm.ControllerName = "Issues";

            return View("List",vm);
        }
    }
}
